#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fa (p>>1)
#define ls (p<<1)
#define rs (p<<1|1)
#define mid ((l+r)>>1)
#define ms0(a) memset(a,0,sizeof a)
#define msp(a) memset(a,0x3f,sizeof a)
#define msn(a) memset(a,-0x3f,sizeof a)
#define mc(a,b) memcpy(a,b,sizeof b)
#define endl '\n'
#define pii pair<int,int>

constexpr int mod=1e9+7;
constexpr int N=140000+7,M=1e6+7;
// constexpr int mx=1000000;
bool bg_m;
int Case=1;
inline void cmax(int &a,int b){a=a<b?b:a;}
inline void cmin(int &a,int b){a=a>b?b:a;}
inline int qp(int a,int b){int res=1;
while(b){if(b&1)res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}return res;}

int n,m,k;
int a[N],b[N];
int c;
vector<int> que[N];
string lv[18];
int cur[18];
int x[4];
int ans[N<<1];
set<int> win[N<<1];
int id[N<<1];
int mxwin[N<<1];
bool who[N<<1];
void dfs(int p,int l,int r,int dep){
    if(dep<k) who[p]=lv[dep][cur[dep]++]-'0';
    // cout<<who[p]<<" "<<p<<endl;
    win[p].clear();
    ans[p]=(l+r)*(r-l+1)/2;
    for(int i=l;i<=r;i++) win[p].insert(i);
    if(l==r){
        mxwin[l]=1;
        id[l]=p;
        return;
    }
    dfs(ls,l,mid,dep+1);
    dfs(rs,mid+1,r,dep+1);
}
void finish(int p,int x){
    while(p){
        if(win[p].find(x)==win[p].end()) return;
        ans[p]-=x;
        win[p].erase(x);
        p>>=1;
    }
}
int solve(int p,int l,int r,int dep){
    if(l==r) return l;
    int L=solve(ls,l,mid,dep-1);
    int R=solve(rs,mid+1,r,dep-1);
    if(who[p]) return ans[p]=(a[R]>=dep?R:L);
    else return ans[p]=(a[L]>=dep?L:R);
}

void Main(){

    cin>>x[0]>>x[1]>>x[2]>>x[3];
    ms0(cur);
    for(int i=1;i<=n;i++) a[i]=b[i]^x[i&3];
    dfs(1,1,(1<<k),0);
    solve(1,1,(1<<k),k);
    int res=0;
    for(int i=1;i<=n;i++){
        int Pos=log2(i);
        if((1<<Pos)<i) Pos++;
        // if((1<<Pos)==i) cout<<i<<" "<<ans[1<<k-Pos]<<endl;
        for(int j:que[i]) res^=j*ans[1<<k-Pos];
        // for(int j=1;j<=(1<<k);j++) cout<<mxwin[j]<<" ";
        // cout<<endl;
    }
    cout<<res<<endl;
    // cout<<(1^2^15^16)<<endl;

    return;
}
string RdFile="arena";
bool en_m;
signed main(){
    freopen((RdFile+".in").c_str(),"r",stdin);
    freopen((RdFile+".out").c_str(),"w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    k=log2(n);
    if((1<<k)<n) k++;
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<=m;i++) cin>>c,que[c].push_back(i);
    for(int i=k-1;~i;i--) cin>>lv[i];
    cin>>Case;
    while(Case--) Main();
    // cerr<<(&en_m-&bg_m)/1024.0/1024<<endl;
    return 0;
}
/*
g++ arena_b.cpp -o arena_b -g -Wall -Wextra -Wshadow -fsanitize=address,undefined -std=c++14
g++ arena.cpp -o arena ‐O2 ‐std=c++14 ‐static
*/